# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.5 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.5m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.5m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.5/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=2.77
_minorchar=a
[[ -z ${_minorchar} ]] && {
  pkgver=${_basever}
} || {
  pkgver=${_basever}.${_minorchar}
}
_pyver=3.5
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
license=('GPL')
# buid conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-llvm35")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
#optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !debug)
install=blender-${CARCH}.install
source=(https://download.blender.org/source/$_realname-${_basever}${_minorchar}.tar.gz
        0001-mingw-cmake.patch
        0002-fix-mingw-w64-definitions.patch
        0003-mingw-dont-install-3rdparty.patch
        0004-fix-finding-glew.patch
        0005-fix-define-syntax.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0007-mingw-DEBUG-dont-suffix-libs-with-_d.patch
        0008-mingw-use-X11-like-path-layout.patch
        0009-osl-deprecate-api.patch
        0010-cast-errors.patch)
sha256sums=('3770fa00f50a6654eb8b5fe625ca8942ab5672ac4685b7af24597251ace85c67'
            'd44da503a554f2084b5cf1538fe549d2780cdb62fc27ebb9c734efef9027ba0e'
            '2bdcaf70d2a64aa3c56c8089c2ecb7b14253ef160b2247a25a1ab34ab5a73e23'
            '9ac81ace5d40a860ea6aa72d3212cc1b9096c350835d0ef8c81b8010fbb02a4c'
            '0cd0fb76502e8325db0400ab1845f91fd0525ff76a22294c3e8031314fb4430f'
            '87036a005a817bce9edd5fb7f18ba3b7a0b8d309d6f1f9bb0f813c931d889c0d'
            '8449c0769ff16b00189715f12275b25442e2b6b7ef4e47797d3229c79b1bb35b'
            '279b72f26df5b4c60a3d7df5bb7b36eb6bf724ef1c3bf925f391239436f39119'
            '0e421c48679d2f0e0183b7c0daf86976cff0be135792ce6605788ba75b720d49'
            '232efc29695e6a2dbbd423bb6fb888bcbd8e73c666a2f791576effd92398b666'
            '2ad48f1a2f0401945e872de95e8bd5e637cd49bb3e027254dff0f1afbe7e48fd')

prepare() {
  cd ${srcdir}/${_realname}-${_basever}${_minorchar}
  patch -p1 -i "${srcdir}"/0001-mingw-cmake.patch
  patch -p1 -i "${srcdir}"/0002-fix-mingw-w64-definitions.patch
  patch -p1 -i "${srcdir}"/0003-mingw-dont-install-3rdparty.patch
  patch -p1 -i "${srcdir}"/0004-fix-finding-glew.patch
  patch -p1 -i "${srcdir}"/0005-fix-define-syntax.patch
  patch -p1 -i "${srcdir}"/0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
  patch -p1 -i "${srcdir}"/0007-mingw-DEBUG-dont-suffix-libs-with-_d.patch
  patch -p1 -i "${srcdir}"/0008-mingw-use-X11-like-path-layout.patch
  patch -p1 -i "${srcdir}"/0009-osl-deprecate-api.patch
  patch -p1 -i "${srcdir}"/0010-cast-errors.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir "${srcdir}"/build-${CARCH}  && cd "${srcdir}"/build-${CARCH}

  [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF"
  [[ $CARCH == i686 ]] && BUILDCUDA="OFF" || BUILDCUDA="ON"
  #[[ $CARCH == i686 ]] && PMARCH="i686" || PMARCH="x86-64"
  #CFLAGS="-O2 -pipe -march=${PMARCH} -mtune=generic"
  #CXXFLAGS="$CFLAGS"
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_OPENJPEG=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_GAMEENGINE=ON \
    -DWITH_RAYOPTIMIZATION=ON \
    -DWITH_JACK=OFF \
    -DWITH_PLAYER=ON \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=python${_pyver}m \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${_pyver}m \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DPYTHON_NUMPY_PATH=${MINGW_PREFIX}/lib/python${_pyver}/site-packages \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_IMAGE_REDCODE=ON \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}-${_basever}${_minorchar}

  # Remove the openjpeg line once blender accepts my patch
  make #-j1 VERBOSE=1
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR=${pkgdir} install
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}"
  ${MINGW_PREFIX}/bin/python3 -m compileall \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${_basever}/scripts/addons
}
